$(function () {
    var tbody=window.document.getElementById("tbody-result");
    $.ajax({
        type:'GET',
        dataType:'json',
        url:contextPath+"learner/all",
        success:function (data) {
            // console.log(data.count);
            // console.log(data);
            var str="";
            // var l=data.message.length();
            // console.log(l);
            for (var i=0;i<data.count;i++) {
                // console.log(data.list[i].course_name);
                str += "<tr>" +
                    "<td>" + i + "</td>" +
                    "<td>" + data.list[i].name+"</td>"+
                    "<td>" + data.list[i].phone+ "</td>" +
                    "<td>" + data.list[i].sex + "</td>" +
                    "<td>" + data.list[i].userid + "</td>" +
                    "<td><button onclick='chuan(this)'>查看资料</button><button onclick='chuanv(this)'>查看视频</button><button onclick='chuand(this)'>查看文档</button><button onclick='save(this)'>保存</button><button onclick='update(this)'>修改</button><button onclick='del(this)'>删除</button></td>" +
                    "</tr>";
            }
            // console.log(str);
            tbody.innerHTML=str;
        }
        ,
        error:function () {
            alert("查询失败")
        }
    })
});
function chuand(r) {
    var table =window.document.getElementById("tbody-result");
    var i= r.parentNode.parentNode.rowIndex-1;
    var ss =new Object();
    ss.name=table.rows[i].cells[1].innerHTML;
    // var data=[];
    // data.push(ss.name);
    window.location.href="userdocumentQuery.html?name="+ss.name;
}
function chuanv(r) {
    var table =window.document.getElementById("tbody-result");
    var i= r.parentNode.parentNode.rowIndex-1;
    var ss =new Object();
    ss.name=table.rows[i].cells[1].innerHTML;
    // var data=[];
    // data.push(ss.name);
    window.location.href="uservideoQuery.html?name="+ss.name;
}
function chuan(r) {
    var table =window.document.getElementById("tbody-result");
    var i= r.parentNode.parentNode.rowIndex-1;
    var ss =new Object();
    ss.name=table.rows[i].cells[1].innerHTML;
    // var data=[];
    // data.push(ss.name);
    window.location.href="studentdetail.html?name="+ss.name;
    // $.ajax({
    //     type:'GET',
    //     url:contextPath+"learner/info/"+ss.name,
    //     success:function (data) {
    //         console.log(data);
    //         window.location.href="studentdetail.html?name="+ss.name;
    //         // window.location.href="studentdetail.html?name="+data.name+"&phone="+data.phone+"&sex="+data.sex+"&userid="+data.userid+"&coursedir="+data.coursedir;
    //     },
    //     error:function () {
    //         alert("查看失败");
    //     }
    // })

}
function save(r) {
    var table=window.document.getElementById("tbody-result");
    var i=r.parentNode.parentNode.rowIndex-1;
    var ss = new Object();
    // ss.name=table.rows[i].cells[1].innerHTML;
    var phone = document.getElementById("uphone").value;
    ss.name=document.getElementById("uname").value;
    ss.sex=document.getElementById("usex").value;
    ss.userid=document.getElementById("userid").value;
    $.ajax({
        type:'PUT',
        url:contextPath+"learner/update/"+phone,
        async: false,
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(ss),
        dataType:"json",
        success:function (data) {
            // table.deleteRow(i);
            alert(data.message)
            window.location.reload();
        },
        error:function () {
            alert("删除失败");
        }
    })

}
function update(r) {    //修改页面信息
    var table=window.document.getElementById("tbody-result");
    var rorNums=table.rows.length;
    var i=r.parentNode.parentNode.rowIndex-1;
    // var ss=new Object();
    table.rows[i].cells[1].innerHTML="<input type='text' id='uname'value=" + table.rows[i].cells[1].innerHTML+ " />";
    table.rows[i].cells[2].innerHTML="<input type='text' id='uphone' value=" + table.rows[i].cells[2].innerHTML+ " />";
    table.rows[i].cells[3].innerHTML="<input type='text' id='usex' value=" + table.rows[i].cells[3].innerHTML+ " />";
    table.rows[i].cells[4].innerHTML="<input type='text' id='userid' value=" + table.rows[i].cells[4].innerHTML+ " />";
}

function  del(r) {
    var table=window.document.getElementById("tbody-result");
    var i=r.parentNode.parentNode.rowIndex-1;
   var ss=new  Object();
   ss.phone=table.rows[i].cells[2].innerHTML;
   // console.log(ss.name);
    // document.getElementById("tbody-result").deleteRow(i);
    $.ajax({
        type:'DELETE',
        url:contextPath+"learner/delete/"+ss.phone,
        success:function (data) {
            // table.deleteRow(i);
            alert(data.message)
            window.location.reload();
        },
        error:function () {
            alert("删除失败");
        }
    })
}
$('#btn').on('click',function () {
    var table=document.getElementById("tbody-result");
    var ss=new Object();
    ss.name=document.getElementById("menuname").value;
    $.ajax({
        type:'GET',
        url:contextPath+"find/learner/"+ss.name,
        // async: false,
        // contentType: "application/json; charset=utf-8",
        // data:JSON.stringify(ss),
        // dataType:"json",
        success:function (data) {
            var str="";
            // var l=data.message.length();
            // console.log(l);
            for (var i=0;i<data.count;i++) {
                // console.log(data.list[i].course_name);
                str += "<tr>" +
                    "<td>" + i + "</td>" +
                    "<td>" + data.list[i].name+"</td>"+
                    "<td>" + data.list[i].phone+ "</td>" +
                    "<td>" + data.list[i].sex + "</td>" +
                    "<td>" + data.list[i].userid + "</td>" +
                    "<td><button onclick='chuan(this)'>查看资料</button><button onclick='chuanv(this)'>查看视频</button><button onclick='chuand(this)'>查看文档</button><button onclick='save(this)'>保存</button><button onclick='update(this)'>修改</button><button onclick='del(this)'>删除</button></td>" +
                    "</tr>";
            }
            // console.log(str);
            table.innerHTML=str;
        },
        error:function () {
            alert("查询失败");
        }
    })
});


